package com.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/9/14
 **/
@Configuration
@SuppressWarnings("all")
public class RedisTemplateConfig {

    @Bean
    public StringRedisTemplate redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(factory);
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringRedisSerializer);
        template.setValueSerializer(jdkSerializationRedisSerializer);
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(jdkSerializationRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    /**
     *序列化redis对象
     */
//    @Autowired
//    public RedisTemplateConfig(StringRedisTemplate redisTemplate) {
//        //1、创建jackson序列化方式
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//        //2、创建Object mapper
//        ObjectMapper objectMapper = new ObjectMapper();
//        //3、允许访问对象中所有属性
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        //4、转换json过程中保存类的信息
//        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);
//        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//        //5、设置key和value的序列化规划  key(String) value(String,list,Set,map)
//        StringRedisSerializer hashKeySerializer = new StringRedisSerializer();
//        redisTemplate.setKeySerializer(hashKeySerializer);
//        //6、jackson2JsonRedisSerializer就是json序列号规则
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        //6.1 设置hash类型key、value序列化方式
//        redisTemplate.setHashKeySerializer(hashKeySerializer);
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//        //7、工厂创建redisTemplate对象之后再进行配置
//        redisTemplate.afterPropertiesSet();
//    }

}
